package com.wzcl.app.controller;


import com.wzcl.app.dao.model.Material;
import com.wzcl.app.model.common.MyPage;
import com.wzcl.app.model.common.RVO;
import com.wzcl.app.model.dto.MaterialDto;
import com.wzcl.app.model.vo.MaterialVo;
import com.wzcl.app.service.MaterialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

@Log4j2
@RestController
@RequestMapping("/material")
@Api(value = "物品管理controller", tags = {"物品管理接口"})
public class MaterialController {

    @Autowired
    private MaterialService materialService;

    @ApiOperation("添加物品信息")
    @PostMapping("/add")
    public RVO<MaterialVo> addMaterial(@RequestBody @Valid MaterialDto materialDto){
        Material material = materialService.addMaterial(materialDto);
        return RVO.success(MaterialVo.create(material));
    }

    @ApiOperation("删除物品信息")
    @PostMapping("/del")
    public RVO<MaterialVo> delMaterial(@RequestBody MaterialDto materialDto){
        Material material = materialService.delMaterialById(materialDto.getId());
        return RVO.success(MaterialVo.create(material));
    }

    @ApiOperation("修改物品信息")
    @PostMapping("/update")
    public RVO<MaterialVo> updateMaterial(@RequestBody @Valid MaterialDto materialDto){
        Material material = materialService.updateMaterial(materialDto);
        return RVO.success(MaterialVo.create(material));
    }

    @ApiOperation("获取物品信息")
    @PostMapping("/get")
    public RVO<MaterialVo> getMaterial(@RequestBody MaterialDto materialDto){
        Material material = materialService.getMaterialById(materialDto.getId());
        if(material != null){
            return RVO.success(MaterialVo.create(material));
        }
        return RVO.success(null);
    }

    @ApiOperation("获取物品信息list")
    @PostMapping("/getList")
    public RVO<List<MaterialVo>> getMaterialList(@RequestBody MaterialDto materialDto){
        List<Material> list = materialService.getMaterialList(materialDto);
        if(list != null && list.size() > 0){
            List<MaterialVo> collect = list.stream().map(MaterialVo::create).collect(Collectors.toList());
            return RVO.success(collect);
        }
        return RVO.success(null);
    }

    @ApiOperation("获取物品信息page")
    @PostMapping("/getPage")
    public RVO<List<MaterialVo>> getMaterialPage(@RequestBody MaterialDto materialDto){
        MyPage<Material> materialPage = materialService.getMaterialPage(materialDto);
        if(materialPage != null && materialPage.getContent().size() > 0){
            List<MaterialVo> collect = materialPage.getContent().stream().map(MaterialVo::create).collect(Collectors.toList());
            return RVO.success(collect,materialPage);
        }
        return RVO.success(null);
    }
}
